<?php

namespace App\Http\Controllers\Admin;

use App\Http\Common\Lib\Common;
use App\Models\League;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class LeagueController extends BaseAdminController
{
    //
    private $model;
    public function __construct()
    {
        parent::__construct();
        $this->model = new League();
    }

    /**
     * 列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View|\think\response\View
     */
    public function index(){
        if(\request()->ajax()){
            $page = \request()->input('page',1);
            $pageSize = \request()->input('pageSize',20);
            $search = \request()->input('search','');
            $where = [
                ['and'=>['status','>',-1]],
            ];
            if($search){
                $where[] = ['like'=>['name','%'.$search.'%']];
            }
            $field = ['id','name','status'];
            $pageData = $this->model->getPages($page,$pageSize,$where,$field,['created_at'=>'desc']);
            return response()->json(['status'=>$pageData['status'],'msg'=>$pageData['msg'],'page'=>$page,'totalCount'=>$pageData['data']['totalCount'],'pages'=>$pageData['data']['pages']]);

        }else{
            return view('admin.league.index');
        }
    }

    /**
     * 添加
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\think\response\View
     */
    public function add(){
        return view('admin.league.add');
    }
    public function addStore(){
        $input = [
            'name'=>\request()->input('name',''),
            'status'=>\request()->input('status',1),
        ];
        $res = $this->model->add($input);
        return response()->json(Common::dataFormat($res['status'],$res['msg'],$res['data']));
    }

    /**
     * 编辑
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\think\response\View
     */
    public function edit(){
        return view('admin.league.edit');
    }
    public function editStore(){
        $input = [
            'id'=>\request()->input('id',0),
            'name'=>\request()->input('name',''),
            'status'=>\request()->input('status',1),
        ];
        $res = $this->model->edit($input);
        return response()->json(Common::dataFormat($res['status'],$res['msg'],$res['data']));
    }

    /**
     * 修改状态
     * @return \Illuminate\Http\JsonResponse
     */
    public function status(){
        $id = \request()->input('id',0);
        $opt = \request()->input('opt','');
        $res = $this->model->changeStatus($id,$opt);
        return response()->json(Common::dataFormat($res['status'],$res['msg'],$res['data']));
    }

    /**
     * 详情
     * @return \Illuminate\Http\JsonResponse
     */
    public function detail(){
        $id = \request()->input('id',0);
        $res = $this->model->findOne($id,['id','name','status']);
        return response()->json(Common::dataFormat($res['status'],$res['msg'],$res['data']));
    }

    /**
     * 列表，用于下拉框
     * @return \Illuminate\Http\JsonResponse
     */
    public function selectList(){
        $lists = $this->model->where('status',1)->select(['id','name'])->get();
        return response()->json(Common::dataFormat(config('statusInfo.status.success'),config('statusInfo.msg.success'),$lists));
    }
}
